package com.tianma315.core.procedures.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;


import com.tianma315.core.common.base.CoreServiceImpl;
import com.tianma315.core.common.exception.MessageException;
import com.tianma315.core.constant.StateEnum;
import com.tianma315.core.procedures.dao.ProceduresDao;
import com.tianma315.core.procedures.domain.ProceduresDO;
import com.tianma315.core.procedures.service.ProceduresService;
import com.tianma315.core.procedures.vo.ProceduresVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;


/**
 * 
 * <pre>
 * 溯源流程模板
 * </pre>
 * <small> 2019-08-13 16:34:19 | Lgc</small>
 */
@Service
public class ProceduresServiceImpl extends CoreServiceImpl<ProceduresDao, ProceduresDO> implements ProceduresService {

    @Autowired
    private ProceduresDao proceduresDao;

    /**
     *
     * @param pageNumber
     * @param pageSize
     * @param proceduresDTO
     * @return
     */
    @Override
    public Page<ProceduresVO> selectPage(Integer pageNumber, Integer pageSize, ProceduresDO proceduresDTO) {
        // 查询列表数据
        Page<ProceduresVO> page = new Page<>(pageNumber, pageSize);
        List<ProceduresVO> proceduresVOList = proceduresDao.selectPage(page,proceduresDTO.getMerchantId());
        page.setRecords(proceduresVOList);
        return page;
    }

    @Override
    public Boolean addProcedures(ProceduresDO proceduresDO) {
        proceduresDO.setCreateDate(new Date());
        proceduresDO.setState(StateEnum.not_del.getCode());
        Wrapper<ProceduresDO> wrapper = new EntityWrapper<>();
        wrapper.eq("name",proceduresDO.getName());
        List<ProceduresDO> proceduresDOS = proceduresDao.selectList(wrapper);
        if (!proceduresDOS.isEmpty()){
            throw new MessageException("此模板名称已添加");
        }

        Boolean result = insert(proceduresDO);
        return result;
    }

    @Override
    public Boolean updateProcedures(ProceduresDO proceduresDO) {
        ProceduresDO procedures = getProceduresDOByProcedureIdAndProductId(proceduresDO);
        if (procedures != null){
            throw new MessageException("此商品已添加模板");
        }
        procedures = getProceduresDOByIsAllProduct(proceduresDO);
        if (procedures != null){
            throw new MessageException("已有模板绑定所有产品");
        }
        Boolean result = updateById(proceduresDO);
        return result;
    }

    @Override
    public Boolean deleteProcedures(Integer id) {
        ProceduresDO proceduresDO = new ProceduresDO();
        proceduresDO.setState(StateEnum.is_del.getCode());
        proceduresDO.setProcedureId(id);
        Boolean result = updateById(proceduresDO);
        return result;
    }

    @Override
    public List<ProceduresDO> selectList(ProceduresDO proceduresDTO) {
        EntityWrapper<ProceduresDO> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("state",StateEnum.not_del.getCode());
//        entityWrapper.eq("merchant_id",proceduresDTO.getMerchantId());
        List<ProceduresDO> list = selectList(entityWrapper);
        return list;
    }

    @Override
    public ProceduresDO getProcedureDO(Integer id) {
        ProceduresDO procedureDO  = selectById(id);
        return procedureDO;
    }

    @Override
    public ProceduresDO getProceduresDOByProductId(Integer productId) {
        EntityWrapper<ProceduresDO> wrapper = new EntityWrapper<>();
        wrapper.eq("product_id",productId);
        wrapper.eq("state",StateEnum.not_del.getCode());
        ProceduresDO proceduresDO = selectOne(wrapper);
        return proceduresDO;
    }

    public ProceduresDO getProceduresDOByProcedureIdAndProductId(ProceduresDO proceduresDO) {
        EntityWrapper<ProceduresDO> wrapper = new EntityWrapper<>();
        wrapper.eq("product_id",proceduresDO.getProductId());
        wrapper.ne("procedure_id",proceduresDO.getProcedureId());
        wrapper.eq("state",StateEnum.not_del.getCode());
        ProceduresDO procedures = selectOne(wrapper);
        return procedures;
    }

    public ProceduresDO getProceduresDOByIsAllProduct(ProceduresDO proceduresDO) {
        EntityWrapper<ProceduresDO> wrapper = new EntityWrapper<>();
        wrapper.eq("is_all_product",1);
        if (proceduresDO.getProcedureId()!=null){
            wrapper.ne("procedure_id",proceduresDO.getProcedureId());
        }
        wrapper.eq("state",StateEnum.not_del.getCode());
        ProceduresDO procedures = selectOne(wrapper);
        return procedures;
    }
}
